﻿<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
                xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
                version="1.0" exclude-result-prefixes="xsl msxsl ddwrt"
                xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                xmlns:ddwrt2="urn:frontpage:internal"
                ddwrt:oob="true"
                xmlns:ie="ie" >

  <!--Calculate what a read mark would look like for our current user id and store it in a global variable-->
  <xsl:variable name="ReadMark" select="concat('.', $Userid,'.')" />

  <xsl:template name="FieldRef_ReadUnreadField_header" ddwrt:dvt_mode="header" match="FieldRef[@FieldType='ReadUnreadField']" mode="header">
    <!--We need to store the configuration of our column here so that rows can be properly displayed
    this is a ViewFields node with a ViewField for each of our fields
    this is currently a HACK that needs to be udpated when SharePoint fixes the bug for 
    querying custom properties, until then we store them in the Direction field except that dir="ltr"-->
    <xsl:param name="thisNode" select="."/>
    <xsl:variable name="RUColumnDescription" select="@Direction"/>
    <xsl:variable name="RUColumnRenderMode" select="substring-before($RUColumnDescription, '#')"/>
    <xsl:variable name="tRemainder" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#'))"/>
    <xsl:variable name="RUShowInlineEditingTools" select="substring-before($tRemainder, '#')"/>
    <xsl:variable name="tRemainder1" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#'))"/>
    <xsl:variable name="RUUnreadHTMLColor" select="substring-before($tRemainder1, '#')"/>
    <xsl:variable name="tRemainder2" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#',$RUUnreadHTMLColor,'#'))"/>
    <xsl:variable name="RUReadImageUrl" select="substring-before($tRemainder2, '#')"/>
    <xsl:variable name="RUUnreadImageUrl" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#',$RUUnreadHTMLColor,'#',$RUReadImageUrl,'#'))"/>

    <script type="text/javascript">
      var ru_FieldName ="<xsl:value-of select="$thisNode/@Name"/>";
      var ru_ColumnRenderMode = "<xsl:value-of select="$RUColumnRenderMode"/>";
      var ru_UnreadColor = "<xsl:value-of select="$RUUnreadHTMLColor"/>";
      var ru_ReadImageUrl = "<xsl:value-of select="$RUReadImageUrl"/>";
      var ru_UnreadImageUrl = "<xsl:value-of select="$RUUnreadImageUrl"/>";
      <xsl:choose>
        <xsl:when test="$RUShowInlineEditingTools='True'">
          var ru_ShowInlineEditiongTools = true;
        </xsl:when>
        <xsl:otherwise>
          var ru_ShowInlineEditiongTools = false;
        </xsl:otherwise>
      </xsl:choose>
      function proxy_markRowRead(divMarkerId)
      {
      markRowAsRead(divMarkerId);
      }
    </script>
    <script  type="text/javascript" src="/_layouts/Voodoo.ListItemReadUnreadSiteColumn/ReadUnread.js" />
    <xsl:choose>
      <xsl:when test="$RUColumnRenderMode='BoldDisplay'">
        <xsl:choose>
          <xsl:when test="$RUShowInlineEditingTools='True'">
            <th  width="29" nowrap="nowrap" scope="col">
            </th>
          </xsl:when>
          <xsl:otherwise>
            <th  width="1" nowrap="nowrap" scope="col">
            </th>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$RUShowInlineEditingTools='True'">
            <th  width="50" nowrap="nowrap" scope="col">
            </th>
          </xsl:when>
          <xsl:otherwise>
            <th  width="16" nowrap="nowrap" scope="col">
            </th>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="FieldRef_ReadUnreadField_body" ddwrt:dvt_mode="body" match="FieldRef[@FieldType='ReadUnreadField']" mode="Note_body">
    <xsl:param name="thisNode" select="."/>
    <!--We need to store the configuration of our column here so that rows can be properly displayed
    this is a ViewFields node with a ViewField for each of our fields
    this is currently a HACK that needs to be udpated when SharePoint fixes the bug for 
    querying custom properties, until then we store them in the Direction field-->
    <xsl:variable name="RUColumnDescription" select="@Direction"/>
    <xsl:variable name="RUColumnRenderMode" select="substring-before($RUColumnDescription, '#')"/>
    <xsl:variable name="tRemainder" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#'))"/>
    <xsl:variable name="RUShowInlineEditingTools" select="substring-before($tRemainder, '#')"/>
    <xsl:variable name="tRemainder1" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#'))"/>
    <xsl:variable name="RUUnreadHTMLColor" select="substring-before($tRemainder1, '#')"/>
    <xsl:variable name="tRemainder2" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#',$RUUnreadHTMLColor,'#'))"/>
    <xsl:variable name="RUReadImageUrl" select="substring-before($tRemainder2, '#')"/>
    <xsl:variable name="RUUnreadImageUrl" select="substring-after($RUColumnDescription, concat($RUColumnRenderMode,'#',$RUShowInlineEditingTools,'#',$RUUnreadHTMLColor,'#',$RUReadImageUrl,'#'))"/>
    <!--This is a "big string" match, so we only do it once for performance reasons 0 = True-->
    <xsl:variable name="RURowReadMarkValue" >
      <xsl:choose>
        <xsl:when test="contains($thisNode/@*[name()=current()/@Name], $ReadMark)">
          0
        </xsl:when>
        <xsl:otherwise>
          1
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <table style="width: 100%;" cellpadding="0" border="0" cellspacing="0"  id="readMarkerRow{$thisNode/@ID}">
      <tr>
        <td style="vertical-align:middle;">
          <xsl:choose>
            <xsl:when test="$RUColumnRenderMode='BoldDisplay'">
              <xsl:choose>
                <xsl:when test="$RURowReadMarkValue=0">
                  <script type="text/javascript">
                    markRowAsRead("readMarkerRow<xsl:value-of select='$thisNode/@ID'/>");
                  </script>
                </xsl:when>
                <xsl:otherwise>
                  <script type="text/javascript">
                    markRowAsUnread("readMarkerRow<xsl:value-of select='$thisNode/@ID'/>");
                  </script>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <!--in Iconic mode we know if its read or unread we can get it started without calling the javascript-->
                <xsl:when test="$RURowReadMarkValue=0">
                  <img  id="readUnreadImage_{$thisNode/@ID}" style="vertical-Align:middle;" width="16" height = "16" border="0" src="{$RUReadImageUrl}"/>
                </xsl:when>
                <xsl:otherwise>
                  <img id="readUnreadImage_{$thisNode/@ID}" style="vertical-Align:middle;" width="16" height = "16" border="0" src="{$RUUnreadImageUrl}"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </td>
        <td style="vertical-align:middle;">
          <xsl:if test="($RUShowInlineEditingTools='True')">
            <xsl:choose>
              <xsl:when test="$thisNode/@FSObjType='1'">
                <!--Folder selected-->
                <span style="display: none">
                  <menu type="ServerMenu" compactmode="true" id="inlineReadUnReadMenu_{$thisNode/@ID}" >
                    <ie:menuitem type="option" menugroupid="100" iconSrc="/_layouts/images/checkall.gif" text="Mark all read" id="inlineMarkAllRead_{$thisNode/@ID}"  onmenuclick="javascript:MarkItemReadOrUnread(true,{$thisNode/@ID},'readMarkerRow{$thisNode/@ID}');"  description="Mark all items and sub items and folders in this folder as read" ></ie:menuitem>
                    <ie:menuitem type="option" menugroupid="100" iconSrc="/_layouts/images/unchecka.gif" text="Mark all unread" id="inlineMarkAllRead_{$thisNode/@ID}"  onmenuclick="javascript:MarkItemReadOrUnread(false,{$thisNode/@ID},'readMarkerRow{$thisNode/@ID}');"  description="Mark all items and sub items and folders in this folder as unread" ></ie:menuitem>
                  </menu>
                </span>
                <span title="Folder marking menu">
                  <div id="inlineReadUnReadMenu_T_{$thisNode/@ID}"
                       oncontextmenu="this.click(); return false;"
                       nowrap="nowrap"
                       onclick="javascript:MMU_Open(byid('inlineReadUnReadMenu_{$thisNode/@ID}'), MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}'),event,false, null, 0);"
                       foa="MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}')" >
                    <a id="inlineReadUnReadMenu_A_{$thisNode/@ID}" accesskey="L"  href="#" style="cursor: hand;white-space: nowrap;" oncontextmenu="this.click(); return false;"
                        onclick="javascript:MMU_Open(byid('inlineReadUnReadMenu_{$thisNode/@ID}'), MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}'),event,false, null, 0);" >
                      <img  border="0" src="/_layouts/images/Voodoo.ListItemReadUnreadSiteColumn/clscFolderMenu16_alt.png" alt="Folder marking menu" />
                    </a>
                  </div>
                </span>
              </xsl:when>
              <xsl:otherwise>
                <!--Item selected-->
                <span style="display: none">
                  <menu type="ServerMenu" compactmode="true" id="inlineReadUnReadMenu_{$thisNode/@ID}" >
                    <ie:menuitem type="option" menugroupid="100" iconSrc="/_layouts/images/checkall.gif" text="Mark item read" id="inlineMarkRead_{$thisNode/@ID}"  onmenuclick="javascript:MarkItemReadOrUnread(true,{$thisNode/@ID},'readMarkerRow{$thisNode/@ID}');"  description="Mark this item as read" ></ie:menuitem>
                    <ie:menuitem type="option" menugroupid="100" iconSrc="/_layouts/images/unchecka.gif" text="Mark item unread" id="inlineMarkRead_{$thisNode/@ID}"  onmenuclick="javascript:MarkItemReadOrUnread(false,{$thisNode/@ID},'readMarkerRow{$thisNode/@ID}');"  description="Mark this item as unread" ></ie:menuitem>
                  </menu>
                </span>
                <span title="Item marking menu">
                  <div id="inlineReadUnReadMenu_T_{$thisNode/@ID}"
                       oncontextmenu="this.click(); return false;"
                       nowrap="nowrap"
                       onclick="javascript:MMU_Open(byid('inlineReadUnReadMenu_{$thisNode/@ID}'), MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}'),event,false, null, 0);"
                       foa="MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}')" >
                    <a id="inlineReadUnReadMenu_A_{$thisNode/@ID}" accesskey="L"  href="#" style="cursor: hand;white-space: nowrap;" oncontextmenu="this.click(); return false;"
                        onclick="javascript:MMU_Open(byid('inlineReadUnReadMenu_{$thisNode/@ID}'), MMU_GetMenuFromClientId('inlineReadUnReadMenu_A_{$thisNode/@ID}'),event,false, null, 0);" >
                      <img  border="0" src="/_layouts/images/Voodoo.ListItemReadUnreadSiteColumn/clscItemMenu16_Alt.png" alt="Item marking menu" />
                    </a>
                  </div>
                </span>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
        </td>
      </tr>
    </table>
  </xsl:template>

</xsl:stylesheet>
